import java.math.BigDecimal;
import java.util.function.BiConsumer;

public class FundsDemo {

    // 通用的资金更新方法
    public static void updateFunds(Long accountId, BigDecimal amount, BiConsumer<AccountFundsPO, BigDecimal> updateFundsFunction) {
        // 模拟从数据库查出账户对象
        AccountFundsPO account = findAccountById(accountId);

        System.out.println("修改前余额: " + account.getBalance());

        // 执行你传进来的更新逻辑（这是关键）
        updateFundsFunction.accept(account, amount);

        // 模拟保存到数据库
        saveAccount(account);
    }

    // 模拟查账户
    public static AccountFundsPO findAccountById(Long accountId) {
        AccountFundsPO account = new AccountFundsPO();
        account.setBalance(new BigDecimal("1000.00"));
        return account;
    }

    // 模拟保存账户
    public static void saveAccount(AccountFundsPO account) {
        System.out.println("修改后余额: " + account.getBalance());
    }

    // 主函数测试
    public static void main(String[] args) {

        Long accountId = 1L;

        // 示例 1：扣款 100 元
        updateFunds(accountId, new BigDecimal("100.00"), (account, amount) -> account.setBalance(account.getBalance().subtract(amount)));

        // 示例 2：加款 200 元
        updateFunds(accountId, new BigDecimal("200.00"), (account, amount) -> account.setBalance(account.getBalance().add(amount)));
    }

    // 模拟账户类
    static class AccountFundsPO {
        private BigDecimal balance = BigDecimal.ZERO;

        public BigDecimal getBalance() {
            return balance;
        }

        public void setBalance(BigDecimal balance) {
            this.balance = balance;
        }
    }
}
